package com.zidian.study.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.zidian.study.adapter.EvaluateAdapter;
import com.zidian.study.bean.EvaluateTeacherBean;
import com.zidian.study.globa.BaseFragment;
import com.zidian.study.globa.Constant;
import com.zidian.study.utils.GetUserInfo;
import com.zidian.study.utils.ToastUtil;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import com.zidian.student.trucks.R;

/**
 * className：
 */
public class EvaluateFragment extends BaseFragment {

    @Bind(R.id.no_data_view)
    LinearLayout noDataView;

    RecyclerView mRecyclerView;
    SwipeRefreshLayout swipeRefreshWidget;
    boolean isLoading;
    private List<EvaluateTeacherBean.DataBean> data = new ArrayList<>();
    private EvaluateAdapter adapter;
    private Handler handler = new Handler();

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        adapter = new EvaluateAdapter(getActivity(), data);
        mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list);
        swipeRefreshWidget = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_widget);
        initView();
        initData();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.tab_fragment_evaluate;
    }


    public static EvaluateFragment newInstance() {

        return new EvaluateFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }


    public void initView() {


        swipeRefreshWidget.setColorSchemeResources(R.color.green);

        swipeRefreshWidget.post(new Runnable() {
            @Override
            public void run() {
                swipeRefreshWidget.setRefreshing(true);
            }
        });
        swipeRefreshWidget.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        data.clear();
                        getData();
                    }
                }, 2000);
            }
        });


        final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(adapter);

    }

    public void initData() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                getData();
            }
        }, 1500);

    }

    /**
     * 获取测试数据
     */
    private void getData() {


        String loginUrl = Constant.URL_TEACHER + "EvaluateByStudent/pushLabelToStudent";
        HashMap<String, String> map = new HashMap<>();
        map.put("studentId", GetUserInfo.getStudentId(mActivity));
        map.put("token", GetUserInfo.getToken(mActivity));
        OkHttpUtils.post().url(loginUrl).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e("E", e.toString());
            }

            @Override
            public void onResponse(String response, int id) {

                try {
                    JSONObject jsonObject = null;
                    jsonObject = new JSONObject(response);
                    int resultCode = jsonObject.getInt("code");

                    if (resultCode != 200) {
                        swipeRefreshWidget.setRefreshing(false);
                        noDataView.setVisibility(View.VISIBLE);
                        ToastUtil.showToast(mActivity, jsonObject.getString("message"));
                        return;
                    } else {
                        Gson gson = new Gson();
                        EvaluateTeacherBean model = gson.fromJson(response, EvaluateTeacherBean.class);
                        if (model.isSuccess()) {
                            if (model.getData().size() < 10) {
                                isLoading = true;
                                adapter.setHasMore(false);
                            } else {
                                isLoading = false;
                                adapter.setHasMore(true);
                            }

                            data.addAll(model.getData());
                            adapter.notifyDataSetChanged();
                            swipeRefreshWidget.setRefreshing(false);
                            adapter.notifyItemRemoved(adapter.getItemCount());
                            if (noDataView != null) {
                                noDataView.setVisibility(View.GONE);
                            }
                        }
                    }

                } catch (JSONException e) {

                }

                Log.e("T", response);


            }
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
